
Daush.Gui = {
	history: [],
	historyIndex: 0,
	promptString: "guest@daush:~$&nbsp;",
	contentElement: null,
	inputText: null,
	intepreter: null,
	_completionObject: null,
	endTabCompletion: function(){
	    this._completionObject = null;
	},
	fireTabCompletion: function (){
	    var intp = Daush.Interpreter;

	    if(!this._completionObject){
		var curCmd = this.inputText.value;
		var mods = intp.mods;
		this._completionObject = { "index":0, "items":[ curCmd ] };
		for(var i in mods){
		    if(mods[i].name.indexOf( curCmd )==0){
			this._completionObject.items.push( mods[i].name + " " );
		    }
		}
	    }
	    var co = this._completionObject;
	    co.index = (co.index + 1) % co.items.length;
	    this.inputText.value = co.items[co.index];
	},
	initialize: function(contentElement) {
		var cookie = Daush.Cookie.readCookie("history");
		console.debug("cookie " + cookie);
		if (cookie != null)
		{			
			try 
			{
				console.debug("history = " + cookie);
				eval("Daush.Gui.history = " + cookie);
				Daush.Gui.historyIndex = Math.max(Daush.Gui.history.length, 0);
				Daush.Gui.log("Load history from cookie!<br/>");
			}
			catch(err)
			{
				console.debug(err.message);
			}
		}

		document.body.onclick = function() {
			Daush.Gui.inputText.focus();
			window.scrollTo(0, 100500);
		};
		this.contentElement = contentElement;
		this.inputText = document.createElement('input');
		this.inputText.id = 'cmdline';
		this.inputText.setAttribute('type', 'text');
		this.inputText.className = 'cmdline';
		this.inputText.onkeydown = function(e) {
			var gui = Daush.Gui;
			var itprt = Daush.Interpreter;

			e = e || event;
			if( e.keyCode != 9 ) gui.endTabCompletion();
			switch (e.keyCode)
			{				
				case 9: // tab
					gui.fireTabCompletion();
					return false;
					break;

				case 13: // enter
					var val = Daush.Gui.inputText.value;
					if (val != "")
					{
						Daush.Gui.disable_input();
						Daush.Gui.history.push(val);
						Daush.Gui.historyIndex = Math.max(Daush.Gui.history.length, 0);

						var str = "[";
						for (var i = Math.max(0, Daush.Gui.history.length - 255); i < Daush.Gui.history.length; i++) 
						{
							str += '"' + Daush.Gui.history[i] + '"';
							if (i < Daush.Gui.history.length - 1)
								str += ",";
						}
						str += "]";
						console.debug(str);
						Daush.Cookie.createCookie("history", str, 1);
					}

					var promptText = document.createElement('div');
					promptText.innerHTML = Daush.Gui.promptString + val;
					$('screenarea').appendChild(promptText);
					window.scrollTo(0, 100500);

					if (val != "")
					{
						Daush.Interpreter.run(val, Daush.Gui.callback);
					}			
					
					break;

				case 38: // up arrow
					console.debug(Daush.Gui.historyIndex);
					if (Daush.Gui.history.length == 0) return;
					Daush.Gui.historyIndex = Math.max(Daush.Gui.historyIndex - 1, 0);
					Daush.Gui.inputText.value = Daush.Gui.history[Daush.Gui.historyIndex];
					break;

				case 40: // down arrow
					console.debug(Daush.Gui.historyIndex);
					if (Daush.Gui.history.length == 0) return;
					Daush.Gui.historyIndex = Math.max(Math.min(Daush.Gui.historyIndex + 1, Daush.Gui.history.length), 0);
					if (Daush.Gui.historyIndex < Daush.Gui.history.length)
					{
						Daush.Gui.inputText.value = Daush.Gui.history[Daush.Gui.historyIndex];
					} else
					{
						Daush.Gui.inputText.value = "";
					}
					break;

			}
		}

		this.promptText = document.createElement('div');
		this.promptText.id = 'prompt';
		this.promptText.className = 'prompt';
		this.promptText.innerHTML = this.promptString;
		
		$('prompt_area').appendChild(this.promptText);
		$('input_area').appendChild(this.inputText);
		//this.contentElement.appendChild(this.promptText);
		//this.contentElement.appendChild(this.inputText);
	},
	add_interpreter: function() {	
	},
	endjob: function() {
	},
	enable_input: function() {
		document.getElementById('input').style.display = '';
		Daush.Gui.inputText.focus();
	},
	disable_input: function() {
		Daush.Gui.inputText.value = "";
		$('input').style.display = 'none';
	},
	clear: function() {
		$('screenarea').innerHTML = "";
	},
	callback: function(divElement) {
		Daush.Gui.enable_input();

		if (divElement != null)
		{
			Daush.Gui.contentElement.appendChild(divElement)
			window.scrollTo(0, 100500);
		}
	},
	log: function(message) {
		var element = document.createElement('div');
		element.className = 'log';
		element.innerHTML = message;

		$('screenarea').appendChild(element);
	}
};

